<table class="table table-hover">
    <thead>
        <tr>
            <th>发货单号</th>
            <th>订单号</th>
            <th>追踪号</th>
            <th>建单人</th>
            <th>订单平台</th>
            <th>发货站点</th>
            <th>发货状态</th>
            <th>物流状态</th>
            <th style="max-width: 200px;">备注</th>
            <th>发货时间</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach($paginator as $row)
            <tr>
                <td>{{ $row->delivery_no ?? '' }}</td>
                <td>{{ $row->order_no ?? ''}}</td>
                <td>{{ $row->logistics->tracking_no ?? ''}}</td>
                <td>{{ \App\Util\User::getUsernameById($row->seller_id)  ?? '' }}</td>
                <td>{{ $row->plat->title ?? '' }}</td>
                <td>{{ $row->area->area ?? '' }}</td>
                <td>{{ $row->status <= 5 ? \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_cnStatusOption[$row->status] : '' }}</td>
                <td>{{ \App\Models\CustomerDelivery\CustomerDelivery::$_logisticsStatus[$row->logistics_status] ?? '' }}</td>
                <td>
                    @if($row->status < 5)
                    <a class="grid-editable-remarks" href="#" data-type="text"
                       data-pk="{{ $row->id }}"
                       data-url="{{ '/xqc/customer-delivery/delivery-abroad/'.$row->id }}"
                       data-value="{{ $row->remarks  ?? '' }}">{{ $row->remarks ?? '' }}</a>
                    @else
                        {{ $row->remarks ?? '' }}
                    @endif
                </td>
                <td>{{ $row->delivered_at ?? '' }}</td>
                <td>
                    @if(\Encore\Admin\Facades\Admin::user()->isRole('salesperson') && \Encore\Admin\Facades\Admin::user()->id != $row->seller_id)
                        <a href="/xqc/customer-delivery/delivery-abroad/{{ $row->id }}" class="btn btn-xs btn-primary">查看</a>
                    @else
                        <a href="/xqc/customer-delivery/delivery-abroad/{{ $row->id }}" class="btn btn-xs btn-primary">查看</a>
                        @if($row->status < 5 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-abroad.edit'))
                            <button class="btn btn-xs btn-danger list-common-modal" data-id="{{ $row->id }}" data-action="listEdit">编辑</button>
                        @endif
                        @if($row->status == 3 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-abroad.ship'))
                            <button class="btn btn-xs btn-danger list-common-modal" data-id="{{ $row->id }}" data-action="ship">发货</button>
                        @endif
                        @if($row->status == 3 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-abroad.delete'))
                            <a href="javascript:void(0);" data-id="{{ $row->id }}" class="btn btn-xs btn-danger grid-row-delete">删除</a>
                        @endif

                        @if(($row->status == 5 || $row->status == 6)  && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-abroad.logistics'))
                            <button class="btn btn-xs btn-info list-logistics-modal" data-id="{{ $row->id }}" data-action="listLogistics">更新物流</button>
                        @endif
                        <a href="/xqc/customer-delivery/delivery-abroad/{{ $row->id }}/download" target="_blank" class="btn btn-xs btn-warning">下载</a>
                    @endif
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
<div class="modal fade" id="listLogisticsModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="listCommonModal" tabindex="-1" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" style="width: 60%;">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script language="JavaScript" type="text/javascript">
    $(function () {

        //编辑物流信息
        $('.list-logistics-modal').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            let action = $(this).data('action');
            $("#listLogisticsModal").modal({
                remote : '/xqc/customer-delivery/delivery-abroad-modal?param=' + id + '&action=' + action,
            });
        });

        $("#listLogisticsModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        });

        //编辑，备货，开始备货，发货
        $('.list-common-modal').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            let action = $(this).data('action');

            $("#listCommonModal").modal({
                remote : '/xqc/customer-delivery/delivery-abroad-modal?param=' + id + '&action=' + action,
            });
        });

        $("#listCommonModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        });

        let _ajaxConfirm = function(action,param,resolve){
            $.ajax({
                method: 'post',
                url: '/xqc/customer-delivery/delivery-abroad-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: action,
                    param: param
                },
                success: function (data) {
                    resolve(data);
                }
            });
        };
        //删除
        $('.grid-row-delete').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            swal({
                title: "确定删除？",
                text : "确定要删除此单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('delDelivery',id,resolve)
                    });
                }
            }).then(function(result) {
                let data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success(data.message);
                        $.pjax.reload('#pjax-container');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
        //返回备货
        $('.grid-row-back').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            swal({
                title: "确定返回？",
                text : "确定要返回备货吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('backPreparing',id,resolve)
                    });
                }
            }).then(function(result) {
                let data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success(data.message);
                        $.pjax.reload('#pjax-container');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
        //备货完成
        $('.grid-row-delivery').unbind('click').click(function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            let action = $(this).data('action');
            $.ajax({
                method: 'post',
                url: '/xqc/customer-delivery/delivery-abroad-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: action,
                     param: id
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success(data.message);
                            $.pjax.reload('#pjax-container');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });
        $('.grid-editable-remarks').editable({
            "emptytext":"<i class=\"fa fa-pencil\"><\/i>",
            "showbuttons":false,
            "mode":"inline",
            "name":'remarks'
        });
    });
</script>
